/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActActiveShare.java  2014年9月12日 上午11:32:08 - Guzhu
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaBaseActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.BaseEntity;
import com.android.tiango.baba.entity.ProductModel;
import com.android.tiango.baba.entity.ShareScoreBean;
import com.android.tiango.baba.entity.Task;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.SocialUtils;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.ToastUtils;
import com.umeng.socialize.sso.UMSsoHandler;

public class ActActiveShare extends BaBaseActivity implements OnClickListener {
	private TextView mTvFen;
	public final static int WIN = 0;
	public final static int LOSE = 1;
	private int mType;

	private ImageView mIv_xinglang;
	private ImageView mIv_qqzone;
	private ImageView mIv_friendq;
	private ImageView mIv_tiangou;
	private Button mBt_xinglang_sorc;
	private Button mBt_qqzone_sorc;
	private Button mBt_friendq_sorc;
	private Button mBt_tiangou_sorc;
	private TextView mTv_close;
	private TextView mTv_xinglang;
	private TextView mTv_qqzone;
	private TextView mTv_friendq;
	private TextView mTv_tiangou;
	
	private String mLogId;
	private ProductModel mProductModel;
	private final static int SHAREACT_RESULT = 1198;
	private int mFen;
	private ShareScoreBean scoreBean;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		Intent intent = getIntent();
		mFen = 100;
		if (intent.hasExtra(Constants.TYPE)) {
			mType = intent.getIntExtra(Constants.TYPE, LOSE);
			if (mType == WIN) {
				setContentView(R.layout.layout_yes_rob);
				if (intent.hasExtra(Constants.DATA)) {
					mProductModel = (ProductModel) intent.getSerializableExtra(Constants.DATA);
				}
				if (intent.hasExtra(Constants.LOGID)) {
					mLogId = intent.getStringExtra(Constants.LOGID);
				}
				initWinView();
			} else if (mType == LOSE) {
				setContentView(R.layout.layout_not_rob);
				initLoseView();
			}
		}

	}

	private void initWinView() {
		loadTopicShareScore();
		mTvFen = (TextView) findViewById(R.id.tv_fen);
		mIv_friendq = (ImageView) findViewById(R.id.iv_friendq);
		mBt_friendq_sorc = (Button) findViewById(R.id.bt_friendq_sorc);
		mIv_qqzone = (ImageView) findViewById(R.id.iv_qqzone);
		mBt_qqzone_sorc = (Button) findViewById(R.id.bt_qqzone_sorc);
		mIv_tiangou = (ImageView) findViewById(R.id.iv_tiangouba);
		mBt_tiangou_sorc = (Button) findViewById(R.id.bt_tiangouba_sorc);
		mIv_xinglang = (ImageView) findViewById(R.id.iv_xinglang);
		mBt_xinglang_sorc = (Button) findViewById(R.id.bt_xinglang_sorc);
		mTv_close = (TextView) findViewById(R.id.tv_close);
		mTv_friendq = (TextView) findViewById(R.id.tv_friendq);
		mTv_qqzone = (TextView) findViewById(R.id.tv_qqzone);
		mTv_tiangou = (TextView) findViewById(R.id.tv_tiangouba);
		mTv_xinglang = (TextView) findViewById(R.id.tv_xinglang);
		mTv_close.setOnClickListener(this);
		initData();
	}

	private void initLoseView() {
		mTvFen = (TextView) findViewById(R.id.tv_fen);
		mTv_close = (TextView) findViewById(R.id.tv_close);
		mTv_close.setOnClickListener(this);
		initData();

	}
	
	private void initData(){
		mTvFen.setText(mFen+"");
		if(mType == WIN&&null!=scoreBean){
			mBt_friendq_sorc.setText("+"+scoreBean.getPoint_others());
			mBt_qqzone_sorc.setText("+"+scoreBean.getPoint_others());
			mBt_xinglang_sorc.setText("+"+scoreBean.getPoint_others());
			mBt_tiangou_sorc.setText("+"+scoreBean.getPoint_tiangou());
		}
		
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(ActActiveShare.this, ActShareBeen.class);
		switch (v.getId()) {
		case R.id.tv_close:
			switch (mType) {
			case WIN:
				Intent intent2 = new Intent(ActActiveShare.this,ActConfirm.class);
				intent2.putExtra(Constants.DATA, mLogId);
				intent2.putExtra(Constants.TYPE, ActConfirm.FROM_ACTIVE);
				startActivity(intent2);
				finish();
				break;
			case LOSE:
				finish();
				break;

			default:
				break;
			}
			break;
		case R.id.iv_friendq:
			intent.putExtra(Constants.DATA, mProductModel);
			intent.putExtra(Constants.TYPE, ActShareBeen.TYPE_FRIENDQ);
			startActivityForResult(intent, SHAREACT_RESULT);
			break;
		case R.id.iv_qqzone:
			intent.putExtra(Constants.DATA, mProductModel);
			intent.putExtra(Constants.TYPE, ActShareBeen.TYPE_QQZONE);
			startActivityForResult(intent, SHAREACT_RESULT);
			break;
		case R.id.iv_xinglang:
			intent.putExtra(Constants.DATA, mProductModel);
			intent.putExtra(Constants.TYPE, ActShareBeen.TYPE_XINLANG);
			startActivityForResult(intent, SHAREACT_RESULT);
			break;
		case R.id.iv_tiangouba:
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == SHAREACT_RESULT) {
			if (resultCode != -1) {
				switch (resultCode) {
				case ActShareBeen.TYPE_FRIENDQ:
					mTv_friendq.setText(getStringValue(R.string.info_share_ed));
					mBt_friendq_sorc.setVisibility(View.INVISIBLE);
					mIv_friendq.setOnClickListener(null);
					try {
						mFen+=Integer.valueOf(scoreBean.getPoint_others());
					} catch (Exception e) {
						// TODO: handle exception
					}
					Task.finishTask(Task.taskOthersShare);
					break;
				case ActShareBeen.TYPE_QQZONE:
					mTv_qqzone.setText(getStringValue(R.string.info_share_ed));
					mBt_qqzone_sorc.setVisibility(View.INVISIBLE);
					mIv_qqzone.setOnClickListener(null);
					try {
						mFen+=Integer.valueOf(scoreBean.getPoint_others());
					} catch (Exception e) {
						// TODO: handle exception
					}
					Task.finishTask(Task.taskOthersShare);
					break;
				case ActShareBeen.TYPE_TIANGOU:
					mTv_tiangou.setText(getStringValue(R.string.info_share_ed));
					mBt_tiangou_sorc.setVisibility(View.INVISIBLE);
					mIv_tiangou.setOnClickListener(null);
					try {
						mFen+=Integer.valueOf(scoreBean.getPoint_tiangou());
					} catch (Exception e) {
						// TODO: handle exception
					}
					Task.finishTask(Task.taskTianGouShare);
					break;
				case ActShareBeen.TYPE_XINLANG:
					mTv_xinglang.setText(getStringValue(R.string.info_share_ed));
					mBt_xinglang_sorc.setVisibility(View.INVISIBLE);
					mIv_xinglang.setOnClickListener(null);
					try {
						mFen+=Integer.valueOf(scoreBean.getPoint_others());
					} catch (Exception e) {
						// TODO: handle exception
					}
					Task.finishTask(Task.taskOthersShare);

					break;

				default:
					break;
				}
			} else {

			}
		}
	}

	@Override
	public void onBackPressed() {
		if (mType == WIN) {
			ToastUtils.showMessage("请点击右上角的\"确认收获\"按钮");
		} else {
			super.onBackPressed();
		}
	}
	
	/**
	 * 获取分享成功增加的分数
	 */
	private void loadTopicShareScore(){
		if (!ShareCookie.isLoginAuth()) {
			showActivity(ActLogin.class, false);
			return;
		}
		HttpClientAsync clientAsync = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());
		clientAsync.get(HttpUrl.getUrl(HttpUrl.TOPICSHARESCORE), params, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				dismissWaitingDialog();
				scoreBean = (ShareScoreBean) arg0;
				if(null!=scoreBean){
					initData();
				}
			}

			@Override
			public void onHttpStarted() {
				showWaitingDialog();
			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				dismissWaitingDialog();
				if (StringUtils.isEmpty(arg1)) {
					arg1 = getStringValue(R.string.info_net_error);
				}
				ToastUtils.showMessage(arg1);
			}
		}, ShareScoreBean.class,true);
		
		
	}

}
